package com.own.component.util.excel.util.read.em;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * DateFormatter
 *
 * @author chenxueli
 * @date 2022-11-23 09:17:00
 */
@Slf4j
public class DateFormatter {

    static final String FORMAT_DATE = "yyyy-MM-dd";
    static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    static final String FORMAT_TIME = "HH:mm:ss";

    private static final Map<Short, String> FORMATTER_MAP = new HashMap<>();

    static {
        for (var em : DateResolveEnum.values()) {
            FORMATTER_MAP.put((short) em.getIndex(), em.getFormat());
        }
    }

    /**
     * 获取格式化规则
     *
     * @param index 格式索引
     * @return 格式化规则
     */
    public static String formatter(short index, Date date) {
        var regex = FORMATTER_MAP.get(index);
        if (regex == null) {
            log.error("为获取到对应格式的解析器:{}", index);
            return "";
        }
        return new SimpleDateFormat(regex).format(date);
    }

}
